﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack.Taskbar;
using Microsoft.WindowsAPICodePack.Dialogs;
using System.IO;

namespace Taskbar7
{
    public partial class Form1 : Form
    {
        private TaskbarManager windowsTaskbar = TaskbarManager.Instance;
        private ThumbnailToolbarButton buttonFirst; 
        private static string AppId = "C4F-Taskbar";

        public Form1()
        {
            InitializeComponent();

            // Don't run unless the taskbar extensions are supported (currently Windows 7+).
            if (!TaskbarManager.IsPlatformSupported)
            {
                MessageBox.Show("This sample requires Windows 7 to run");
                Application.Exit();
            }

            // The application ID is used to group windows together
            windowsTaskbar.ApplicationId = AppId;
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            // Creates a button to be shown in the preview (thumbnail) view (hover over the taskbar)
            buttonFirst = new ThumbnailToolbarButton(Properties.Resources.search, "Search");
            buttonFirst.Click += new EventHandler<ThumbnailButtonClickedEventArgs>(buttonFirst_Click);

            // Adds the button to the toolbar for the window (identified by the window handle)
            windowsTaskbar.ThumbnailToolbars.AddButtons(this.Handle, buttonFirst);

            // Populates the drop-down list  with progressbar states directly from the enumeration
            comboBoxProgressState.DataSource = Enum.GetNames(typeof(TaskbarProgressBarState));
            comboBoxProgressState.SelectedIndex = 0;
        }

        private void trackProgress_Scroll(object sender, EventArgs e)
        {
            // Set the progress bar based on the trackbar and update the taskbar to match
            progressBarValue.Value = trackProgress.Value;
            windowsTaskbar.SetProgressValue(trackProgress.Value, 100); // value, max
        }

        private void comboBoxProgressState_SelectedIndexChanged(object sender, EventArgs e)
        {
            // One-line method of updating the taskbar's progress state
            // to the enumerated value selected in the ComboBox
            windowsTaskbar.SetProgressState((TaskbarProgressBarState)Enum.Parse(typeof(TaskbarProgressBarState), comboBoxProgressState.Text));
        }

        private void buttonBrowseIcon_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                // Extract the icon from the selected file and set it as the overlay to the taskbar icon
                // Useful for indicators like error, offline, or update
                Icon selIcon = Icon.ExtractAssociatedIcon(openFileDialog1.FileName);
                windowsTaskbar.SetOverlayIcon(this.Handle, selIcon, "Overlay");
                lblIconFilename.Text = openFileDialog1.FileName;
            }
        }

        private void checkBoxPreviewClip_CheckedChanged(object sender, EventArgs e)
        {
            // Each tabbed thumbnail can be given a clipping region relative to the entire form's bitmap
            // You can set a clipping region to the location and size of a certain control, as here,
            // or choose arbitrary coordinates.
            if (checkBoxPreviewClip.Checked)
            {
                windowsTaskbar.TabbedThumbnail.SetThumbnailClip(this.Handle, new Rectangle(groupBox2.Location, groupBox2.Size));
            }
            else
            {
                windowsTaskbar.TabbedThumbnail.SetThumbnailClip(this.Handle, new Rectangle(0, 0, this.Width, this.Height));
            }
        }

        private void buttonClearOverlay_Click(object sender, EventArgs e)
        {
            // Clear the overlay by simply setting it to null
            windowsTaskbar.SetOverlayIcon(this.Handle, null, null);
            lblIconFilename.Text = "";
        }

        void buttonFirst_Click(object sender, ThumbnailButtonClickedEventArgs e)
        {
            // This event fires when the custom button in the taskbar preview is clicked
            MessageBox.Show("Search icon clicked");
        }

    }
}
